home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 00 - Game Programming Primer / HelloWorld6.1.c < prev    next >
Text File  |  1995-06-29  |  24KB  |  521 lines

  1. //==============================================================================================\\
  2. //        -----------------------------------------------------------------------------------        \\
  3. //        HelloWorld6.1.c version 1.0.0    copyright © 1993…1995 Jamie McCornack, john calhoun            \\
  4. //        -----------------------------------------------------------------------------------        \\
  5. //         Demo program for Macintosh GameWriter 1.0.0, a training program…                        \\
  6. //        …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,…        \\
  7. //        …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment…    \\
  8. //        …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc.    \\
  9. //         A tutorial is available in Tricks of the Mac Game Programming Gurus, published…        \\
  10. //        …by Hayden Books, August 1995.                                                            \\
  11. //                                                                                                \\
  12. //        This code is offered by the copyright holders for no fee and for whatever use…            \\
  13. //        …you care to make of it, but we do hope you remember where it came from.                \\
  14. //                                                                                                \\
  15. //        Please send bug reports to MacGameDev at America OnLine.    macgamedev@aol.com            \\
  16. //        Suggestions and observations are also appreciated.                                        \\
  17. //        Updates and upgrades will be available now and then from the above e-mail address.        \\
  18. //==============================================================================================\\
  19.  
  20. // This program opens a window, displays an 8-bit color background, and at first…
  21. // …mouseclick, runs two clams across the screen. At second mouseclick, one clam stops,…
  22. // …talks, and flaps its/his/her face for 40 frames as the other clam continues running.
  23. // When the other clam runs up to the standing clam, there is an impact.
  24.  
  25. // If the monitor is not set to 8-bit, a soft alert is displayed, asking if the user…
  26. // …would like to switch to 8-bit, or stick with the current setting.
  27.  
  28. // THIS IS HELLOWORLD6, BUT WITH NO DELAY (SEE KWAITTICKS).
  29.  
  30. #include "MGWExterns1.h"
  31.  
  32. #define     kPutInFront    (WindowPtr)-1L
  33. #define     kWaitTicks         0L    // Sets the delay in Ticks between frames. Try 3. Try 2. Try 0.
  34. #define     kJaneStepLength    12    // Sets the distance in pixels between Clamity Jane's moves.
  35. #define     kClemStepLength    16    // Sets the distance in pixels between Clem the Clam's moves.
  36. #define  kFrontFace            0    // Enumerates the various faces of the clam sprites.
  37. #define  kBlinkFace         1
  38. #define  kEehFace            2
  39. #define  kOohFace            3
  40. #define  kStepRightFace        4
  41. #define  kWalkRightFace        5
  42. #define  kRunRightFace        6
  43. #define  kDizzyRightFace1    7
  44. #define  kDizzyLeftFace1    8
  45. #define  kDizzyRightFace2    9
  46. #define  kDizzyLeftFace2    10
  47. #define  kMaxFaces            11
  48. #define  kBounce            -2    // Sets how many steps the running clam bounces back on impact.
  49. #define  kLastFace        40        // Sets how many frames the clams stay visible after impact.
  50.         
  51.                 // The resource constants--with 'r' prefix like Apple wants them.
  52. #define  rJaneFacesID    136        // The 'PICT' ID# where the views of Jane are located.
  53. #define  rClemFacesID    135        // The 'PICT' ID# where the views of Clem are located.
  54. #define  rMasksID        130        // The 'PICT' ID# where the clam masks are located.
  55. #define  rBackgroundID    134        // The 'PICT' ID# where the background picture is located.
  56. #define  rMainWindowID    128        // The 'WIND' resource ID# for the main window.
  57.  
  58. #define  rHelloSndID    3000
  59. #define  rFootstepSndID    3001
  60. #define  rImpactSndID    3002
  61. #define  rDizzySndID    3003
  62.  
  63. #define  kColorBitsPreferred    8
  64.     
  65. typedef struct
  66. {
  67.     Rect    face;
  68.     Rect    mask;
  69. } tSpriteType;
  70.  
  71.  
  72. Rect    bigPictureRect, masksRect,  allComboRect;
  73. Rect    janeFacesRect, janeIsAtRect, janeWasAtRect, janeComboRect;
  74. Rect    clemFacesRect, clemIsAtRect, clemWasAtRect, clemComboRect;
  75. CGrafPtr    workCPort, janeFacesCPort, clemFacesCPort, backgroundCPort;
  76. GrafPtr mainWindow, masksPort;
  77. Boolean    itWorked, contactFlag, impactReadyFlag, gameOverFlag, evenFrame;    // Note new flags.
  78. long    targetTick;
  79. short    janeSprite, clemSprite, thisFaceCounter;
  80.  
  81. tSpriteType    sprite[kMaxFaces];
  82.  
  83. extern    Boolean        gUserWantsSound;
  84.     
  85.     
  86. //==============================================================  Prototypes
  87.  
  88. void InitAll(void);
  89. void OpenMainWindow (void);
  90. void SetTheRects(void);
  91. void SetTheCPorts(void);
  92. void CopyBothAtOnce (void);
  93. void CopyOneAtATime (void);
  94. void ShowClams (void);
  95. void DoDelay (void);
  96. void JaneLoop (void);
  97. void ClemLoop (void);
  98. void JaneSpeaks (void);
  99. void DoImpact (void);
  100.  
  101. //==============================================================  Functions
  102.  
  103. //--------------------------------------------------------------  InitAll
  104.  
  105. void InitAll(void)
  106. {
  107.     InitToolbox();
  108.     if (WhatsOurDepth() < kColorBitsPreferred)    // Compare color depth with what we want.
  109.         YellowAlert(kPref8BitColor);            // If smaller, notify user.
  110.     if (WhatsOurDepth() > kColorBitsPreferred)    // Compare color depth with what we want.
  111.         YellowAlert(kPrefDownTo8BitColor);        // If larger, notify user.
  112.     gUserWantsSound = TRUE;
  113.     InitializeForSound();
  114.     SetTheRects();        // Since some of these Rects define fields in CGrafPorts,…
  115.     SetTheCPorts();        // …set the rects before setting the ports.
  116.     janeSprite = kStepRightFace;
  117.     clemSprite = kRunRightFace;
  118.     thisFaceCounter = 0;
  119.     contactFlag = FALSE;        // Not touching,
  120.     impactReadyFlag = FALSE;    // Not about to collide,
  121.     gameOverFlag = FALSE;        // Not done playing.
  122.     evenFrame = TRUE;
  123.     targetTick = TickCount() + kWaitTicks;
  124.     HideCursor();        // This demo doesn't use mouse input, so why look at it?
  125. }
  126.  
  127. //--------------------------------------------------------------  OpenMainWindow
  128.  
  129. void OpenMainWindow (void)
  130. {
  131.     mainWindow = GetNewCWindow(128, 0L, kPutInFront);    // Load window from resource.
  132.     ShowWindow((GrafPtr)mainWindow);                    // Now display it.
  133.     SetPort((GrafPtr)mainWindow);                        // Make its port current.
  134.     ClipRect(&bigPictureRect);                            // Set its clip region.
  135.     CopyRgn(mainWindow->clipRgn, mainWindow->visRgn);    // Set its visRgn.
  136.     ForeColor(blackColor);                                // Set its pen color to black.
  137.     BackColor(whiteColor);                                // Set background color white.
  138. }
  139.  
  140. //--------------------------------------------------------------  SetTheRects
  141.  
  142. void SetTheRects(void)    // The most tedious part of programming this type of game.
  143. {
  144.     SetRect(&janeFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  145.     SetRect(&clemFacesRect, 0, 0, 448, 64);        // Size and shape of BitMap for the sprite faces.
  146.     SetRect(&masksRect, 0, 0, 448, 32);        // Size and shape of BitMap for the sprite masks.
  147.     SetRect(&bigPictureRect, 0, 0, 512, 322);    // The shape of the picture we'll put in the main window, workCPort and backgroundCPort.}
  148.     SetRect(&janeIsAtRect, 80, 240, 112, 272);    // The shape (32 x 32) of the images of Jane, and the position of the first image.}
  149.     janeWasAtRect = janeIsAtRect;                // Initializing janeIsAtRect...it has to start somewhere, and this is handy.}
  150.     janeComboRect = janeIsAtRect;
  151.     SetRect(&clemIsAtRect, 40, 244, 72, 276);    // The shape (32 x 32) of the images of Clem, and the position of the first image.}
  152.     clemWasAtRect = clemIsAtRect;                // Initializing clemIsAtRect...it has to start somewhere, and this is handy.}
  153.     clemComboRect = clemIsAtRect;
  154.         // And now, the tedium. In this sample, all we're doing is showing the clam running across the screen to the right.}
  155.         // However, if you use ResEdit and look at 'PICT' 129 in Sample.rsrc, you'll find 28 different views of the clam.}
  156.         // If we wanted the clam to run left too, and walk slowly, and face the user, and blink its eyes, we'd be calling…}
  157.         // …SetRect 56 times--one face and one mask per sprite. And if we had jumping clams and rear views of clams…}
  158.         // …and starfish and clamdiggers and other hazards of the clam environment, we might have hundreds of rects to set.}
  159.     SetRect(&sprite[kFrontFace].face, 320, 32, 352, 64);    // The shape and position of sprite[kFrontFace].face on facesCPort.portPixMap.
  160.     SetRect(&sprite[kFrontFace].mask, 320, 0, 352, 32);    // The shape and position of sprite[kFrontFace].mask on masksPort.portPixMap.
  161.     SetRect(&sprite[kBlinkFace].face, 320, 0, 352, 32);    // Note that some faces (e.g. eyes open or closed) use the same mask,…
  162.     SetRect(&sprite[kBlinkFace].mask, 320, 0, 352, 32);    // …since they have the same silhouette.}
  163.     SetRect(&sprite[kEehFace].face, 352, 0, 384, 32);    // I could write more comments here, but setting these rects…
  164.     SetRect(&sprite[kEehFace].mask, 352, 0, 384, 32);    // …is already tedious enough without a bunch of busy-work.
  165.     SetRect(&sprite[kOohFace].face, 352, 32, 384, 64);
  166.     SetRect(&sprite[kOohFace].mask, 352, 0, 384, 32);    
  167.     SetRect(&sprite[kStepRightFace].face, 192, 0, 224, 32);    
  168.     SetRect(&sprite[kStepRightFace].mask, 192, 0, 224, 32);    
  169.     SetRect(&sprite[kWalkRightFace].face, 224, 0, 256, 32);    
  170.     SetRect(&sprite[kWalkRightFace].mask, 224, 0, 256, 32);    
  171.     SetRect(&sprite[kRunRightFace].face, 160, 0, 192, 32);    // BTW, there are plenty more clam faces and masks in the 'PICT's,…
  172.     SetRect(&sprite[kRunRightFace].mask, 160, 0, 192, 32);    // …if you feel you need rect setting practice.  :-)
  173.     SetRect(&sprite[kDizzyRightFace1].face, 384, 0, 416, 32);        // Hey look! Here's more now!
  174.     SetRect(&sprite[kDizzyRightFace1].mask, 384, 0, 416, 32);    
  175.     SetRect(&sprite[kDizzyRightFace2].face, 384, 32, 416, 64);    
  176.     SetRect(&sprite[kDizzyRightFace2].mask, 384, 0, 416, 32);    
  177.     SetRect(&sprite[kDizzyLeftFace1].face, 416, 0, 448, 32);    
  178.     SetRect(&sprite[kDizzyLeftFace1].mask, 416, 0, 448, 32);    
  179.     SetRect(&sprite[kDizzyLeftFace2].face, 416, 32, 448, 64);    
  180.     SetRect(&sprite[kDizzyLeftFace2].mask, 416, 0, 448, 32);    
  181.     
  182. }
  183.  
  184. //--------------------------------------------------------------  SetTheCPorts
  185.  
  186. void SetTheCPorts(void)    // Create the CGrafPorts and load their .portPixMap fields.
  187. {
  188.             // Create BitMap for sprite masks. NOTE THIS IS A BITMAP!
  189.     CreateOffScreenBitMap (&masksRect, &masksPort);
  190.     LoadGraphic (rMasksID);        // …load 'PICT' resource for the clam masks.
  191.     
  192.             // Create PixMap for Jane faces.
  193.     CreateOffScreenPixMap (&janeFacesRect, &janeFacesCPort);
  194.     LoadGraphic (rJaneFacesID);        // …load 'PICT' resource for the clam faces.
  195.  
  196.             // Create PixMap for Clem faces.
  197.     CreateOffScreenPixMap (&clemFacesRect, &clemFacesCPort);
  198.     LoadGraphic (rClemFacesID);        // …load 'PICT' resource for the clam faces.
  199.  
  200.             // Create PixMap for the background.
  201.     CreateOffScreenPixMap (&bigPictureRect, &backgroundCPort);
  202.     LoadGraphic(rBackgroundID);    // …load 'PICT' resource for the background picture.
  203.     
  204.             // Create PixMap for offscreen graphics work.
  205.     CreateOffScreenPixMap (&bigPictureRect, &workCPort);
  206.  
  207.     OpenMainWindow();
  208.  
  209.         //{This fills the main window with the background picture, so the user can see it.
  210.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  211.         &((GrafPtr)mainWindow)->portBits, 
  212.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  213.  
  214. // This fills the workCPort.portPixMap with the background picture, so updates can be done quickly.
  215.     CopyBits(&((GrafPtr)backgroundCPort)->portBits,
  216.         &((GrafPtr)workCPort)->portBits, 
  217.         &bigPictureRect, &bigPictureRect, srcCopy, mainWindow->visRgn);
  218. }
  219.  
  220. //--------------------------------------------------------------  CopyBothAtOnce
  221.  
  222. void CopyBothAtOnce (void)    //Display the clams on the screen when they're close together.
  223. {
  224.         //UnionRect(clemComboRect, larryComboRect, allComboRect);
  225.         //{Find the smallest rectangle which will cover Larry and Clem.}
  226.     UnionRect(&janeComboRect, &clemComboRect, &allComboRect);
  227. // Find the smallest rectangle which will cover the old position of Clem and the new.
  228.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  229.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  230.         &(((GrafPtr)mainWindow)->portBits), 
  231.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  232.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, allComboRect, allComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  233.         //{Restore the workPort by covering up our clams with the background they obscure.  This way, workPort is…}
  234.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  235.         &(((GrafPtr)workCPort)->portBits), 
  236.         &allComboRect, &allComboRect, srcCopy, mainWindow->visRgn);
  237. // Restore the workCPort by covering up our clam with the background it obscures.
  238. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  239. // without having to copy the entire PixMap.
  240. }
  241.  
  242. //--------------------------------------------------------------  CopyOneAtATime
  243.  
  244. void CopyOneAtATime (void)    // Display the clams on the screen when they're far apart.
  245. {
  246.         //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, larryComboRect, larryComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  247.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  248.         &(((GrafPtr)mainWindow)->portBits), 
  249.         &janeComboRect, &janeComboRect, srcCopy, mainWindow->visRgn);
  250. // Copy the contents of janeComboRect from workCPort->portPixMap to the main window. In one swell foop, old Jane…
  251. //…will be erased, and the new Jane overlayed onto the background picture. Wallah! Flicker-free animation!
  252.     //CopyBits(BitMapPtr(workMapC^.portPixMap^)^, GrafPtr(mainWndo)^.portBits, clemComboRect, clemComboRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  253.     CopyBits(&((GrafPtr)workCPort)->portBits, 
  254.         &(((GrafPtr)mainWindow)->portBits), 
  255.         &clemComboRect, &clemComboRect, srcCopy, mainWindow->visRgn);
  256. // Copy the contents of clemComboRect from workCPort->portPixMap to the main window. In one swell foop, old Clem
  257. //…will be erased, and the new Clem overlayed onto the background picture. Wallah! Flicker-free animation!
  258.     //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, larryRect, larryRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  259.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  260.         &(((GrafPtr)workCPort)->portBits), 
  261.         &janeIsAtRect, &janeIsAtRect, srcCopy, mainWindow->visRgn);
  262. // Restore the workCPort by covering up Jane's image with the background it obscures.
  263. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  264. // without having to copy the entire PixMap.
  265.     CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  266.         &(((GrafPtr)workCPort)->portBits), 
  267.         &clemIsAtRect, &clemIsAtRect, srcCopy, mainWindow->visRgn);
  268. // Restore the workCPort by covering up Clem's image with the background it obscures.
  269. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  270. // without having to copy the entire PixMap.
  271.         //CopyBits(BitMapPtr(backgroundMapC^.portPixMap^)^, BitMapPtr(workMapC^.portPixMap^)^, clemRect, clemRect, srcCopy, GrafPtr(mainWndo)^.visRgn);
  272. }
  273.  
  274. //--------------------------------------------------------------  ShowClam 
  275.  
  276. void ShowClams (void)    // Do the animation and make it appear on the screen.
  277. {
  278.     Rect dummyRect;
  279.     
  280.     CopyMask(&((GrafPtr)janeFacesCPort)->portBits, 
  281.         &((GrafPtr)masksPort)->portBits, 
  282.         &((GrafPtr)workCPort)->portBits, 
  283.         &sprite[janeSprite].face, 
  284.         &sprite[janeSprite].mask, 
  285.         &janeIsAtRect);
  286. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  287. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  288. // previous image of the clam visible on the screen while we worked.
  289.  
  290.     UnionRect(&janeWasAtRect, &janeIsAtRect, &janeComboRect);
  291. // Find the smallest rectangle which will cover the old position of Jane and the new.
  292.  
  293. CopyMask(&((GrafPtr)clemFacesCPort)->portBits, 
  294.         &((GrafPtr)masksPort)->portBits, 
  295.         &((GrafPtr)workCPort)->portBits, 
  296.         &sprite[clemSprite].face, 
  297.         &sprite[clemSprite].mask, 
  298.         &clemIsAtRect);
  299. // Now there is an image of a clam in the new position in workMap.  If we had done this work in…
  300. // mainWindow, we would have seen considerable flickering.  Instead, we did it offscreen, and left the…
  301. // previous image of the clam visible on the screen while we worked.
  302.  
  303.     UnionRect(&clemWasAtRect, &clemIsAtRect, &clemComboRect);
  304. // Find the smallest rectangle which will cover the old position of Clem and the new.
  305.  
  306.  
  307.     if (SectRect(&clemIsAtRect, &janeIsAtRect, &dummyRect))
  308.     {
  309.         contactFlag = TRUE;
  310.         CopyBothAtOnce();
  311.     }
  312.     else
  313.     {
  314.         contactFlag = FALSE;
  315.         CopyOneAtATime();
  316.     }
  317. }
  318. //    CopyBits(&((GrafPtr)workCPort)->portBits, 
  319.         //&(((GrafPtr)mainWindow)->portBits), 
  320.         //&clamComboRect, &clamComboRect, srcCopy, mainWindow->visRgn);
  321. // Copy the contents of comboRect from workCPort->portPixMap to the main window. In one swell foop, the old clam…}
  322. //…will be erased, and the new clam overlayed onto the background picture. Wallah! Flicker-free animation!}
  323.  
  324.     //CopyBits(&((GrafPtr)backgroundCPort)->portBits, 
  325.         //&(((GrafPtr)workCPort)->portBits), 
  326.         //&clamIsAtRect, &clamIsAtRect, srcCopy, mainWindow->visRgn);
  327. // Restore the workCPort by covering up our clam with the background it obscures.
  328. // This way, workCPort->portPixMap is identical to backgroundCPort->portPixMap,…
  329. // without having to copy the entire PixMap.
  330. //}
  331.  
  332. //--------------------------------------------------------------  DoDelay
  333.  
  334. // This is the companion function to the above function (LogNextTick()).
  335. // We do nothing but loop until TickCount() catches up with (or passes) our…
  336. // global variable tickNext.
  337.  
  338. void DoDelay (void)
  339. {
  340.     do
  341.     {
  342.     }
  343.     while (TickCount() < targetTick);            // Loop until TickCount() catches up.
  344.     targetTick = TickCount() + kWaitTicks;
  345. }
  346.  
  347. //--------------------------------------------------------------  JaneLoop
  348.  
  349. void JaneLoop (void)
  350. {
  351.     switch    (janeSprite)                    //If the current view of jane is…
  352.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  353.         {    janeSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  354.             break;    }
  355.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  356.         {    janeSprite = kRunRightFace;        // …kRunRightFace.
  357.             break;    }
  358.     // And if it was neither kStepRightFace nor kWalkRightFace, then janeSprite was either…
  359.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  360.             janeSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  361.     }
  362.     
  363.     janeWasAtRect = janeIsAtRect;            // Store the clam's current position as its last position,…
  364.                                             // …we'll be erasing it next time through the loop.
  365.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position--it'll be…
  366.                                             // …kStepLength pixels to the right of its last position.
  367.     if (janeIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  368.     {                                        // …set the right border of clamIsAtRect…
  369.         janeIsAtRect.right = 0;                // …to the left edge of the screen…
  370.         janeIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  371.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  372. }
  373.  
  374. //--------------------------------------------------------------  ClemLoop
  375.  
  376. void ClemLoop (void)
  377. {
  378.     switch    (clemSprite)                    //If the current view of Clem is…
  379.     {    case    (kStepRightFace):             // …kStepRightFace, then set thisSprite to…
  380.         {    clemSprite = kWalkRightFace;    // …kWalkRightFace, and if it is currently…
  381.             break;    }
  382.         case    (kWalkRightFace):             // …kWalkFace, then set it to…
  383.         {    clemSprite = kRunRightFace;        // …kRunRightFace.
  384.             break;    }
  385.     // And if it was neither kStepRightFace nor kWalkRightFace, then clemSprite was either…
  386.         default                    :             // …kRunRightFace, or what it was when RunRight()…
  387.             clemSprite = kStepRightFace;    // …was called, so we set it to kStepRightFace
  388.     }
  389.     clemWasAtRect = clemIsAtRect;            // Store the clam's current position as its last position,…
  390.                                             // …we'll be erasing it next time through the loop.
  391.     OffsetRect(&clemIsAtRect, kClemStepLength, 0);    // Set the clam's next position--it'll be…
  392.                                             // …kStepLength pixels to the right of its last position.
  393.     if (clemIsAtRect.left > 512)                // If the clam has wandered out of sight,…
  394.     {                                        // …set the right border of clamIsAtRect…
  395.         clemIsAtRect.right = 0;                // …to the left edge of the screen…
  396.         clemIsAtRect.left = -32;            // …and move the left border of clamIsAtRect…
  397.     }                                        // …as needed to maintain its 32 x 32 shape & size.
  398.     if (clemSprite == kStepRightFace)                // Only sound a footstep in pose where foot strikes ground.
  399.           PlayASound(rFootstepSndID, kLowSoundPriority);        //Ahh, the pitter patter of tiny feet.
  400. }
  401.  
  402. //--------------------------------------------------------------  JaneSpeaks
  403.  
  404. // This routine has the clam stop moving, face the screen, and move its face.
  405.  
  406. void JaneSpeaks (void)
  407. {    Rect dummyRect;
  408.     if ((impactReadyFlag) && (SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))
  409.         DoImpact();
  410.     thisFaceCounter = thisFaceCounter + 1;
  411.          switch (thisFaceCounter)
  412.          {    case    (1): 
  413.             {    janeSprite = kFrontFace;
  414.                 break;    }
  415.             case    (2): 
  416.             {    janeSprite = kEehFace;
  417.                 break;    }
  418.             case    (3):     // This keeps an impact from occurring when both clams are in the same…
  419.             {    if (!(SectRect(&janeIsAtRect, &clemIsAtRect, &dummyRect)))    // …place when the…
  420.                     impactReadyFlag = TRUE;                                    // …button is pushed.
  421.                 break;    }
  422.             case    (4): 
  423.             {    janeSprite = kFrontFace;
  424.                 break;    }
  425.             case    (5): 
  426.             {    janeSprite = kOohFace;
  427.                 break;    }
  428.             case    (9): 
  429.             {    janeSprite = kEehFace;
  430.                 break;    }
  431.             case    (11): 
  432.             {    janeSprite = kOohFace;
  433.                 break;    }
  434.             case    (13): 
  435.             {    janeSprite = kFrontFace;
  436.                 break;    }
  437.             case    (16): 
  438.             {    janeSprite = kBlinkFace;
  439.                 break;    }
  440.             case    (18): 
  441.             {    janeSprite = kFrontFace;
  442.                 break;    }
  443.             case    (35): 
  444.             {    janeSprite = kBlinkFace;
  445.                 break;    }
  446.             case    (38): 
  447.             {    janeSprite = kFrontFace;
  448.                 break;    }
  449.             case    (39): 
  450.             {    impactReadyFlag = TRUE;
  451.                 break;    }
  452.         }
  453. }
  454.  
  455. //--------------------------------------------------------------  DoImpact
  456.  
  457. void DoImpact (void)
  458. {
  459.     janeWasAtRect = janeIsAtRect;    // Store the clam's current position as its last position.
  460.     OffsetRect(&janeIsAtRect, kJaneStepLength, 0);    // Set the clam's next position.
  461.     clemWasAtRect = clemIsAtRect;        // Store the clam's current position as its last position.
  462.     OffsetRect(&clemIsAtRect, kClemStepLength * kBounce, 0);    // Set the clam's next position.
  463.     PlayASound(rImpactSndID, kHighestSoundPriority);        // High enough to interrupt anything.
  464.     thisFaceCounter = 0;
  465.     while (thisFaceCounter <= kLastFace)
  466.     {
  467.         ShowClams();
  468.         DoDelay();                // Do nothing for a while.
  469.         PlayLoopSound(rDizzySndID, kHighSoundPriority);    // Not high enough to interrupt the impact.
  470.         thisFaceCounter = thisFaceCounter + 1;
  471.         evenFrame = !evenFrame;
  472.         if (evenFrame)
  473.         {
  474.             janeSprite = kDizzyLeftFace1;
  475.             clemSprite = kDizzyRightFace1;
  476.         }
  477.         else
  478.         {
  479.             janeSprite = kDizzyLeftFace2;
  480.             clemSprite = kDizzyRightFace2;
  481.         }
  482.     }
  483.     gameOverFlag = TRUE;        // And we're outta here. Game over.
  484. }
  485.  
  486. //--------------------------------------------------------------  main
  487. //----------------------------------------------------------------------
  488.  
  489. void main(void)
  490. {
  491.     InitAll();
  492.     while (!Button())    // Before the user presses the mouse button, nothing happens.
  493.         {
  494.         }
  495.     while (Button())    // When the user presses the mouse button, nothing happens.
  496.         {
  497.         }
  498.     while (!Button())    // When the mouse button is released, continue to…
  499.         {
  500.         ShowClams();    // Put the clams on the screen.
  501.         DoDelay();        // Keep everything from happening too fast.
  502.         JaneLoop();        // Put Jane in her next position.
  503.         ClemLoop();        // Put Clem in his next position.
  504.         }
  505.     PlayASound(rHelloSndID, kMediumSoundPriority);    // Sound, "Hello" when button is pushed.
  506.     while (!(gameOverFlag))    // …until the clams crash into each other and fall down dizzy.
  507.         {    
  508.         ShowClams();    // Put the clams on the screen.
  509.         DoDelay();        // Keep everything from happening too fast.
  510.         JaneSpeaks();    // Make mouth motions, shift into impact mode when ready.
  511.         ClemLoop();        // Put Clem in his next position.
  512.         }
  513.     CloseDownSound();
  514.     InitCursor();    // Rarely needed, since most programs call InitCursor on startup. Still, it's…
  515.                     // a good habit to leave everything in normal condition when your programs quit.
  516. }                    // And we're done.
  517.  
  518. //------------------------------------------------------------------------------------------\\
  519. //                                    End HelloWorld6.1.c                                        \\
  520. //------------------------------------------------------------------------------------------\\
  521.